﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhanCongGiangDay
{
    public class CHocKi
    {
        public static object getAll()
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            var hks = from hk in db.HOCKies
                      select new MHocKi
                      {
                          maHK = hk.MAHK,
                          tenHK = hk.TENHK,
                          namHoc = hk.NAMHOC
                      };
            return hks.ToList();
        }

        public static int addHK(string maHK, string tenHK, string namHoc)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                HOCKY hk = new HOCKY
                {
                   MAHK = maHK,
                   TENHK = tenHK,
                   NAMHOC = namHoc
                };
                db.HOCKies.InsertOnSubmit(hk);
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int deleteBM(string maHK)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                HOCKY hk = db.HOCKies.Where(b => b.MAHK == maHK).Single();
                db.HOCKies.DeleteOnSubmit(hk);
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int updateBM(string maHK, string tenHK, string namHoc)
        {
            
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                HOCKY hk = (from hkObject in db.HOCKies
                            where hkObject.MAHK == maHK
                            select hkObject).First();                
                hk.TENHK = tenHK;
                hk.NAMHOC = namHoc;
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static string getHKById(string p)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                var hks = (from hk in db.HOCKies
                          where hk.MAHK == p
                          select hk.NAMHOC).First();
                return hks.ToString();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
